const {
	Controller
} = require('uni-cloud-router')
const uniID = require('uni-id')
module.exports = class UserController extends Controller {
	async getWithdrawList() {
		let {
			pageNum = 1,
			pageSize = 10
		} = this.ctx.data
		try{
			let result = await this.service.trade.getWithdrawList({
				pageNum,
				pageSize,
				isMember: false,
				userId: this.ctx.auth.uid
			})
			return {
				code: 200,
				result
			}
		}catch(e){
			return {
				code: 500,
				message: e.message
			}
		}
	}
	async changePwd() {
		const data = this.ctx.data
		return uniID.updatePwd({
			uid: this.ctx.auth.uid,
			...data
		})
	}
	
	async getAccount() {
		return this.service.user.getAccount()
	}
	
	async getUserInfo() {
		let res = await this.service.user.getCurrentUserInfo()
		if (!res.code) {
			return {
				code: 200,
				result: res.userInfo
			}
		}
		return {
			code: res.code,
			msg: res.message
		}
	}
	
	async applyWithdraw() {
		let {
			fee,
			password
		} = this.ctx.data
		fee = parseInt(fee)
		if (isNaN(fee)) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		return this.service.user.applyWithdraw(fee, password)
	}
	
	async bindWX() {
		let {
			code,
			wxUserInfo
		} = this.ctx.data
		if (!code || !wxUserInfo) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			let userInfo = await this.service.merchant.bindWX({
				code,
				wxUserInfo
			})
			return {
				code: 200,
				result: userInfo
			}
		}catch(e){
			return {
				code: 501,
				msg: e.message
			}
		}
	}
	
	async unbindWX() {
		try{
			let userInfo = await this.service.merchant.unbindWX()
			return {
				code: 200,
				result: userInfo
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async modifyPwd() {
		let {
			oldPassword,
			newPassword
		} = this.ctx.data
		if (!oldPassword || !newPassword) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			let res = await uniID.updatePwd({
				uid: this.ctx.auth.uid,
				oldPassword,
				newPassword
			})
			if (res.code) {
				return {
					code: 500,
					msg: res.message
				}
			}
			return {
				code: 200
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
}
